package educrypt.gui.components;

import java.awt.Color;
import java.awt.Desktop;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import educrypt.gui.util.GuiUtil;
import educrypt.gui.view.AboutView;
import educrypt.i18n.Messages;
import educrypt.resources.EduCryptResources;

class AboutPanel extends EducryptPanel implements AboutView {

	private static final long serialVersionUID = 5751854768314239866L;
	private JButton btnLanguageSpanish;
	private JButton btnLanguageEnglish;
	private JLabel lblWelcome, lblDescription, lblChangeLanguage;

	private URI uri;

	public AboutPanel() {
		this.setSize(740, 650);
		this.setPreferredSize(this.getSize());
		setLayout(null);

		lblWelcome = new JLabel(Messages.getString("guiWelcome"), JLabel.CENTER);
		lblWelcome.setFont(new Font("Tahoma", Font.BOLD, 28));
		lblWelcome.setBounds(106, 30, 527, 47);
		add(lblWelcome);

		lblDescription = new JLabel(
				Messages.getString("guiEduCryptDescription"), JLabel.CENTER);
		lblDescription.setFont(lblDescription.getFont().deriveFont(14f)
				.deriveFont(Font.PLAIN));

		lblDescription.setBounds(14, 110, 712, 47);
		add(lblDescription);

		lblChangeLanguage = new JLabel(Messages.getString("guiChangeLanguage"));
		lblChangeLanguage.setHorizontalAlignment(SwingConstants.CENTER);
		lblChangeLanguage.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblChangeLanguage.setBounds(59, 480, 649, 19);
		add(lblChangeLanguage);

		ImageIcon iconSpanish = new ImageIcon(
				EduCryptResources.getImageURL("es.png"));

		btnLanguageSpanish = new JButton(iconSpanish);
		btnLanguageSpanish.setBounds(255, 520, 70, 50);
		GuiUtil.setTransparentButtonBackground(btnLanguageSpanish);
		add(btnLanguageSpanish);

		ImageIcon iconEnglish = new ImageIcon(
				EduCryptResources.getImageURL("en.png"));

		btnLanguageEnglish = new JButton(iconEnglish);
		btnLanguageEnglish.setBounds(415, 520, 70, 50);
		GuiUtil.setTransparentButtonBackground(btnLanguageEnglish);
		add(btnLanguageEnglish);

		try {
			this.uri = new URI(Messages.getString("URL"));
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}

		JLabel lblLink = new JLabel(uri.toString(), JLabel.CENTER);
		lblLink.setForeground(new Color(0, 0, 156));// #000099 <- link color
		lblLink.setFont(lblDescription.getFont().deriveFont(14f)
				.deriveFont(Font.PLAIN));
		lblLink.setText(uri.toString());
		lblLink.setToolTipText(uri.toString());
		lblLink.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(java.awt.event.MouseEvent e) {
				open(uri);
			}
		});
		lblLink.setBounds(218, 259, 304, 31);
		add(lblLink);

		JLabel lblWebPage = new JLabel(
				Messages.getString("guiWebPage"), SwingConstants.CENTER); //$NON-NLS-1$
		lblWebPage.setToolTipText("http://code.google.com/p/educrypt/");
		lblWebPage.setFont(lblDescription.getFont().deriveFont(14f)
				.deriveFont(Font.PLAIN).deriveFont(Font.BOLD));
		lblWebPage.setBounds(218, 218, 304, 50);
		add(lblWebPage);

	}

	// Link
	private static void open(URI uri) {
		if (Desktop.isDesktopSupported()) {
			try {
				Desktop.getDesktop().browse(uri);
			} catch (IOException e) {
			}
		} else {
		}
	}

	// END Link

	@Override
	public void addListener(ActionListener ctr) {
		btnLanguageEnglish.setActionCommand(LANG_ENGLISH);
		btnLanguageEnglish.addActionListener(ctr);
		btnLanguageSpanish.setActionCommand(LANG_SPANISH);
		btnLanguageSpanish.addActionListener(ctr);
	}

	@Override
	public int showLanguageChangeConfirmation() {
		return GuiUtil.showQuestionDialog(this,
				Messages.getString("guiResetApplicationQuestion"),
				Messages.getString("guiResetApplication"));
	}
}
